package com.yuexun.dhdoctor.module.commun.ui;

import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.module.Contract.adapter.AddMemberAdapter;
import com.yuexun.dhdoctor.module.Contract.bean.GroupGroupData;
import com.yuexun.dhdoctor.module.Contract.bean.People;
import com.yuexun.dhdoctor.widget.FixedSwipeListView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Administrator on 2017/3/28 0028.
 */

public class Act_DepartAndGroup_Group extends BaseKJActivity {

    @Bind(R.id.title_tv)
    TextView titleTv;
    @Bind(R.id.right)
    CheckBox cbAll;

    @Bind(R.id.etSearch2)
    EditText etSearch2;
    @Bind(R.id.btnSearchClear)
    ImageButton btnSearchClear;
    @Bind(R.id.searchLayout)
    LinearLayout searchLayout;
    @Bind(R.id.people_list)
    FixedSwipeListView peopleList;

    @Bind(R.id.rl_bottom)
    RelativeLayout rlBottom;
    @Bind(R.id.grid)
    GridView grid;
    @Bind(R.id.addmember_sure_btn)
    TextView addmemberSureBtn;

    private Handler handler;
    private AddMemberAdapter addMemberAdapter;
    private ArrayList<People> peoples = new ArrayList<>();
    private ArrayList<People> usersBeans_origin = new ArrayList<>();

    @Override
    public void setRootView() {
        super.setRootView();
        setContentView(R.layout.act_choice_group);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initHandle();
        GroupGroupData groupGroupDataBean = (GroupGroupData) getIntent().getExtras().getSerializable("DataBean");

        titleTv.setText(groupGroupDataBean.getTitle());
        cbAll.setVisibility(View.GONE);
        rlBottom.setVisibility(View.GONE);

        for (int i = 0; i < groupGroupDataBean.getToId().size(); i++) {
            People people = new People();
            people.setUserId(groupGroupDataBean.getToId().get(i) + "");
            people.setUserName(groupGroupDataBean.getToName().get(i) + "");
            people.setPinyin(groupGroupDataBean.getPinyin().get(i));
            people.setJobNum(groupGroupDataBean.getJobNum().get(i));
            peoples.add(people);
        }

        addMemberAdapter = new AddMemberAdapter(aty.getApplicationContext(), peoples, usersBeans_origin, new Handler(), 0, peopleList, true);
        peopleList.setAdapter(addMemberAdapter);

        etSearch2.addTextChangedListener(new TextWatcher() {
            boolean hasPadding = true;

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                if (TextUtils.isEmpty(s) && !hasPadding) {
                    Message msg = new Message();
                    msg.what = 10002;
                    handler.sendMessage(msg);

                    hasPadding = true;
                } else if (!TextUtils.isEmpty(s) && hasPadding) {
                    Message msg = new Message();
                    msg.what = 10001;
                    handler.sendMessage(msg);

                    hasPadding = false;
                }
                if (addMemberAdapter != null) {
                    addMemberAdapter.getMyFilter(null == s ? "" : s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (etSearch2.getText().toString().length() == 0) {
                    searchLayout.setVisibility(View.VISIBLE);
                } else {
                    searchLayout.setVisibility(View.GONE);
                }
            }
        });
    }

    void initHandle() {
        handler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 10001: // 搜索有文字的的时候
                        btnSearchClear.setVisibility(View.VISIBLE);
                        break;
                    case 10002: // 搜索没文字的的时候
                        btnSearchClear.setVisibility(View.INVISIBLE);
                        break;
                }
            }
        };
    }

    @Override
    @OnClick({R.id.dept_back, R.id.btnSearchClear, R.id.addmember_sure_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dept_back:
                onBackPressed();
                break;
            case R.id.btnSearchClear:
                etSearch2.setText("");
                break;
        }
    }
}
